package imper.main;

import java.io.FileInputStream;
import java.io.IOException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.newdawn.slick.SlickException;
import org.newdawn.slick.loading.LoadingList;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class XMLUtil
{
	public static NodeList getLoadList(String pPath, String pNode, boolean deferred) throws SlickException
	{
		DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
		DocumentBuilder docBuilder = null;
		try
		{
			docBuilder = docBuilderFactory.newDocumentBuilder();
		}
		catch (ParserConfigurationException e)
		{
			throw new SlickException("Could not load resources", e);
		}
		Document doc = null;
		try
		{
			doc = docBuilder.parse(new FileInputStream(pPath));
		}
		catch (SAXException e)
		{
			throw new SlickException("Could not load resources", e);
		}
		catch (IOException e)
		{
			throw new SlickException("Could not load resources", e);
		}
		// normalize text representation
		doc.getDocumentElement().normalize();
		NodeList listResources = doc.getElementsByTagName(pNode);
		if (deferred)
		{
			LoadingList.setDeferredLoading(true);
		}
		return listResources;
	}
}